package com.jprocms.module.cms.service.imgblockpic;

import com.jprocms.module.cms.dal.dataobject.imgblock.ImgblockDO;
import com.jprocms.module.cms.enums.EnumImgblockActivePos;
import com.jprocms.module.cms.service.channel.ChannelService;
import com.jprocms.module.cms.service.imgblock.ImgblockService;
import com.jprocms.module.cms.service.site.SiteService;
import com.jprocms.module.cms.service.staticpage.StaticHtmlService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.jprocms.module.cms.controller.admin.imgblockpic.vo.*;
import com.jprocms.module.cms.dal.dataobject.imgblockpic.ImgblockPicDO;
import com.jpro.framework.common.pojo.PageResult;
import com.jpro.framework.common.pojo.PageParam;
import com.jpro.framework.common.util.object.BeanUtils;

import com.jprocms.module.cms.dal.mysql.imgblockpic.ImgblockPicMapper;

import static com.jpro.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.jprocms.module.cms.enums.ErrorCodeConstants.*;

/**
 * 飘窗图片 Service 实现类
 *
 * @author jprocms
 */
@Service
@Validated
public class ImgblockPicServiceImpl implements ImgblockPicService {

    @Resource
    private ImgblockPicMapper imgblockPicMapper;
    @Resource
    private ImgblockService imgblockService;

    @Override
    public Long createImgblockPic(ImgblockPicSaveReqVO createReqVO) {
        // 插入
        ImgblockPicDO imgblockPic = BeanUtils.toBean(createReqVO, ImgblockPicDO.class);
        imgblockPicMapper.insert(imgblockPic);
        imgblockService.staticHtmlPage(null,imgblockService.getImgblock(imgblockPic.getSectorId()));
        // 返回
        return imgblockPic.getId();
    }

    @Override
    public void updateImgblockPic(ImgblockPicSaveReqVO updateReqVO) {
        // 校验存在
        validateImgblockPicExists(updateReqVO.getId());
        // 更新
        ImgblockPicDO updateObj = BeanUtils.toBean(updateReqVO, ImgblockPicDO.class);
        imgblockPicMapper.updateById(updateObj);
        imgblockService.staticHtmlPage(null,imgblockService.getImgblock(updateObj.getSectorId()));
    }

    @Override
    public void deleteImgblockPic(Long id) {
        // 校验存在
        ImgblockPicDO imgblockPicDO = validateImgblockPicExists(id);
        // 删除
        imgblockPicMapper.deleteById(id);
        imgblockService.staticHtmlPage(null,imgblockService.getImgblock(imgblockPicDO.getSectorId()));
    }

    private ImgblockPicDO validateImgblockPicExists(Long id) {
        final ImgblockPicDO imgblockPicDO = imgblockPicMapper.selectById(id);
        if (imgblockPicDO == null) {
            throw exception(IMGBLOCK_PIC_NOT_EXISTS);
        }
        return  imgblockPicDO;
    }
    @Override
    public ImgblockPicDO getImgblockPic(Long id) {
        return imgblockPicMapper.selectById(id);
    }

    @Override
    public PageResult<ImgblockPicDO> getImgblockPicPage(ImgblockPicPageReqVO pageReqVO) {
        return imgblockPicMapper.selectPage(pageReqVO);
    }


}